﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DoxygenBrowser.Common.Model;
using DoxygenBrowserAddin.Utils;
using DoxygenBrowserAddin.Controller;
using DoxygenBrowserAddin.Data;

namespace DoxygenBrowserAddin.Views
{
  public partial class FrmDoxygenConfig : Form
  {
    private DoxygenConfig _doxConfig;
    private string _strSolutionPath;

    public FrmDoxygenConfig(string strSolutionPath)
    {
      InitializeComponent();
      _strSolutionPath = Path.GetDirectoryName(strSolutionPath);
      DoxPathChanged = false;
    }

    public string SolutionPath
    {
      get { return _strSolutionPath; }
      set { _strSolutionPath = value; }
    }

    private bool _doxPathChanged;
    public bool DoxPathChanged
    {
      get { return _doxPathChanged; }
      set { _doxPathChanged = value; }
        
    }

    public void Populate(DoxygenConfig doxyConfig)
    {
      _doxConfig = doxyConfig;

      _txtHTMLDocPath.Text = _doxConfig.DoxHtmlPath;
      _txtSolutionPath.Text = SolutionPath;
      _chkUseFrames.Checked = _doxConfig.UseFrames;
    }

    public DialogResult ShowDialog(DoxygenConfig doxygenConfig)
    {
      Populate(doxygenConfig);
      DialogResult ret = ShowDialog();
      if (ret == DialogResult.OK)
      {
        retrieveConfig(doxygenConfig);
      }
      return ret;
    }

    private void retrieveConfig(DoxygenConfig doxygenConfig)
    {
      doxygenConfig.DoxHtmlPath = _txtHTMLDocPath.Text;
      doxygenConfig.UseFrames = _chkUseFrames.Checked;
    }

    private string getDocFullPath()
    {
      string strFullPath = _txtHTMLDocPath.Text;
      if (!Path.IsPathRooted(strFullPath))
      {
        strFullPath = Path.Combine(_strSolutionPath,strFullPath);
        strFullPath = Path.GetFullPath(strFullPath);
      }
      return strFullPath;
    }

    private void _btnHTMLDocPathBrowser_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog dlg = new FolderBrowserDialog();
      dlg.ShowNewFolderButton = false;
      dlg.SelectedPath = SolutionPath;
      if (dlg.ShowDialog() == DialogResult.OK)
      {
        _txtHTMLDocPath.Text = dlg.SelectedPath;
      }
    }

    private void _txtHTMLDocPath_TextChanged(object sender, EventArgs e)
    {
      string strPath = _txtHTMLDocPath.Text;
      if (Path.IsPathRooted(strPath))
      {
        
        string strRelativePath = ExPath.GetRelativePath(SolutionPath, strPath);
        if (!string.IsNullOrEmpty(strRelativePath))
        {
          _txtHTMLDocPath.Text = strRelativePath;
        }

      }
      DoxPathChanged = true;
    }

 
  }
}
